package org.hhchat.leetcode.math;

/**
 * https://discuss.leetcode.com/topic/59383/0ms-c-solution-with-detail-explanation
 */
public class code400_easy {
    static class Solution {
        public int findNthDigit(int n) {
           long fix = 9;
           long base = 1;
           long digits = 1;
            while (n > fix * digits) {
                n -= fix * digits;
                fix *= 10;
                digits++;
                base *= 10;
            }
            System.out.println(base);
            long num = base + (n % digits == 0 ? n / digits - 1 : n / digits);
            long index = n % digits;
            if (index == 0) {
                index = digits;
            }
            String numStr = String.valueOf(num);
            return Character.getNumericValue(numStr.charAt((int) (index - 1)));
        }

    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        System.out.println(solution.findNthDigit(250));
    }
}
